package com.tc.service.impl;

import com.tc.dto.Category;
import com.tc.dto.Food;
import com.tc.dto.Recommend;
import com.tc.dto.Result;
import com.tc.factory.ResultFactory;
import com.tc.mapper.HomeMapper;
import com.tc.myDto.BusinessInformation;
import com.tc.myDto.BusinessShop;
import com.tc.service.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.*;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    private ResultFactory result;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    //获取所有推荐菜品
    @Override
    public Result randomRecommend() {
        //获取所有商家，以及商家的所有菜品
        //手动更新流程:删除redis2中的所有key-value,然后打开这里的注释
//        List<BusinessShop> list1=homeMapper.randomRecommend();
//        for(int i=0;i<list1.size();i++){
//            redisTemplate.opsForSet().add("businessShop",list1.get(i));
//        }

        //直接从redis中获取所有商家,注意拿到的是list集合
        Set<Object> businessShop = redisTemplate.opsForSet().members("businessShop");
        //把Set集合转为List集合,方便用户随机获取店铺
        List<Object> list=new ArrayList<>(businessShop);
        //打乱集合中的排序
        Collections.shuffle(list);
        //如果店铺数量超过五个则进行随机推送
        if(list.size()>5){
            list=list.subList(0,5);
        }
        //新建推荐对象的容器,直接返回给前端
        List<Recommend> data=new ArrayList<>();
        //解构list集合，重新封装成推荐对象
        for (Object o : list) {
            //店铺信息
            BusinessShop b=(BusinessShop) o;
            //旗下菜品集合
            data.add(new Recommend(b,homeMapper.getAllFood(b.getBusinessName())));
        }
        return result.success(data);
    }

    //根据商家名称获取所有菜品分类
    @Override
    public Result getBusinessInformation(String name) {
        //首先获取商家名称
        List<Category> list=homeMapper.selectBusinessName(name,"菜品分类");
        return result.success(list);
    }

    //根据分类获取菜品
    @Override
    public Result getFoodByCategory(Category category) {
        List<Food> list=homeMapper.getFoodByCategory(category);
        return result.success(list);
    }

    //检查商铺名是否重复
    @Override
    public Boolean checkShopName(String shopName) {
        return homeMapper.checkShopName(shopName)==null? null:true;
    }

    //新增店铺
    @Override
    public void addShop(BusinessInformation businessInformation) {
        BusinessShop businessShop= BusinessShop.builder()
                                               .username(businessInformation.getUsername())
                                               .password(businessInformation.getPassword())
                                               .businessName(businessInformation.getShopName())
                                               .img(businessInformation.getImg())
                                               .bgImg(businessInformation.getBgImg())
                                               .build();
        //一定是先存入redis
        redisTemplate.opsForSet().add("businessShop",businessShop);
        //最后再存入mysql
        homeMapper.addShop(businessShop);
    }
}
